﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using TFSTools.TFSLibrary;

namespace TFSTools.WpfControls
{
    public class TFSTreeItem
    {
        private string name;
        private string fullPath;
        private int depth;

        internal TFSTreeItem(string name, string fullPath, int depth)
        {
            this.name = name;
            this.fullPath = fullPath;
            this.depth = depth;
        }

        public string Name { get { return this.name; } }

        public string FullPath { get { return this.fullPath; } }

        public int Depth { get { return this.depth; } }

        public override string ToString()
        {
            return new string(' ', this.depth) + this.name;
        }
    }

    public class TFSTreeItemFlatList : List<TFSTreeItem>
    {
        internal TFSTreeItemFlatList(TFSTeamProject project, XmlElement rootElement)
        {
            string name = project.Name;
            string fullPath = project.Name;
            int depth = 0;
            TFSTreeItem newItem = new TFSTreeItem(name, fullPath, depth);
            this.Add(newItem);

            XmlNode xmlNode = rootElement.ChildNodes[0];
            if (xmlNode.Name == "Node" && xmlNode.Attributes["Name"].Value == "Iteration" && xmlNode.HasChildNodes)
            {
                foreach (XmlNode childrenNode in xmlNode.ChildNodes)
                {
                    if (childrenNode.Name == "Children" && childrenNode.HasChildNodes)
                    {
                        foreach (XmlNode childNode in childrenNode.ChildNodes)
                        {
                            if (xmlNode.Name == "Node")
                            {
                                this.AddTreeViewNodes(fullPath, depth, childNode);
                            }
                        }
                    }
                }
            }
        }

        private void AddTreeViewNodes(string fullPath, int depth, XmlNode xmlNode)
        {
            string name = xmlNode.Attributes["Name"].Value;
            fullPath = fullPath + @"\" + name;
            depth += 3;

            TFSTreeItem newItem = new TFSTreeItem(name, fullPath, depth);
            this.Add(newItem);

            if (xmlNode.HasChildNodes)
            {
                foreach (XmlNode childrenNode in xmlNode.ChildNodes)
                {
                    if (xmlNode.Name == "Children" && xmlNode.HasChildNodes)
                    {
                        foreach (XmlNode childNode in childrenNode.ChildNodes)
                        {
                            if (xmlNode.Name == "Node")
                            {
                                this.AddTreeViewNodes(fullPath, depth, childNode);
                            }
                        }
                    }
                }
            }
        }
    }
}
